﻿using CompanyManageSystem.DLL;
using CompanyManageSystem.Model;
using CompanyManageSystem.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Repast
{
    public partial class Frm_Unit : BaseXtraForm
    {
        public Frm_Unit()
        {
            InitializeComponent();

            this.BindRowIndicator(UnitView);
            this.BindCheckEdit(UnitView, false, true);           
        }

        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_Unit_Load(object sender, EventArgs e)
        {
            RepastService rservice = new RepastService();
            UnitGrid.DataSource = rservice.GetAllUnit();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_AddUnit frm = new Frm_AddUnit(status:EStatus.Add);
            frm.SaveEvent += Unit_AddSaveEvent;
            frm.ShowDialog();
        }
        private void Unit_AddSaveEvent(Object sender ,EventArgs e)
        {
            RepastService rservice = new RepastService();
            UnitGrid.DataSource = rservice.GetAllUnit();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var focusUnit = UnitView.GetFocusedRow() as Unit;
            if (focusUnit == null)
            {
                ShowInfo("请选择需要编辑的单位!");
                return;
            }

            Frm_AddUnit frm = new Frm_AddUnit(status:EStatus.Modify,id:focusUnit.ID);
            frm.SaveEvent += Unit_UpdateSaveEvent;
            frm.ShowDialog();
        }
        private void Unit_UpdateSaveEvent(Object sender ,EventArgs e)
        {
            RepastService rservice = new RepastService();
            Unit unit = sender as Unit;
            if (unit != null)
            {
                UnitView.SetFocusedRowCellValue("Code", unit.Code);
                UnitView.SetFocusedRowCellValue("Name", unit.Name);
            }
            else
            {
                UnitGrid.DataSource = rservice.GetAllUnit();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RepastService rservice = new RepastService();
            var unit = this.GetChecked<Unit>(UnitView);
            if (unit.Count == 0)
            {
                ShowInfo("请选择需要删除的单位!");
                return;
            }

            var unitId = unit.Select(p => p.ID).ToArray();
            if (ShowQuestion($"是否删除{unit.Count}条数据?"))
            {
                int count = rservice.DelUnit(unitId);
                UnitGrid.DataSource = rservice.GetAllUnit();
                ShowInfo("删除成功!");
            }
        }

        
    }
}
